<?php

namespace app\admin\controller\department;

use app\admin\model\department\Department;
use app\admin\model\department\Department as DepartmentModel;
use app\common\constant\Consts;
use app\common\controller\Backend;
use \app\admin\model\department\Admin as DepartmentAdminModel;
use \app\admin\model\department\AuthAdmin as AdminModel;
use fast\Random;
use fast\Tree;
use Overtrue\Pinyin\Pinyin;
use think\Db;
use think\Exception;
use think\Log;
use think\Validate;
use app\common\constant;

/**
 * 部门成员
 */
class Admin extends Backend
{

    protected $tree = null;
    /**
     * 部门人员对象
     * @var null
     */
    protected $dadminModel = null;
    protected $childrenGroupIds = [];//权限组
    /**
     * 部门所有
     * @var array
     */
    protected $allDepartment = [];//


    public function _initialize()
    {
        parent::_initialize();
        $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin());

        $this->dadminModel = new DepartmentAdminModel;
        $departmentList = [];
        $this->allDepartment = collection(DepartmentModel::allDepartment())->toArray();
        $departmentdata = [];
        foreach ($this->allDepartment as $k => $v) {
            $state = ['opened' => true];
            $departmentList[] = [
                'id' => $v['id'],
                'parent' => $v['parent_id'] ? $v['parent_id'] : '#',
                'text' => __($v['name']),
                'state' => $state
            ];
        }
        $tree = Tree::instance()->init($this->allDepartment, 'parent_id');
        $departmentOptions = $tree->getTree(0, "<option model='@model_id' value=@id @selected @disabled>@spacer@name</option>");

        $this->view->assign('departmentOptions', $departmentOptions);
        $this->assignconfig('departmentList', $departmentList);

        $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
        foreach ($result as $k => $v) {
            $departmentdata[$v['id']] = $v['name'];
        }
        $this->view->assign('departmentdata', $departmentdata);

        //兼容旧版本没有手机号
        $database=config('database');
        $exits_mobile=\think\Db::query("SELECT 1 FROM  information_schema.COLUMNS WHERE  table_name='{$database['prefix']}admin' AND COLUMN_NAME='mobile' limit 1");
        $this->view->assign('exits_mobile', $exits_mobile?1:0);
        $this->assignconfig('exits_mobile', $exits_mobile?1:0);
    }

    /**
     * 成员列表
     */
    public function index()
    {

        //测试
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $department_id = $this->request->request("department_id");
        if ($this->request->isAjax()) {
            $this->model = new \app\admin\model\department\AuthAdmin();
            $filter = $this->request->get("filter", '');
            $filter = (array)json_decode($filter, true);
            $filter_w = [];

            if (isset($filter['department_id'])) {
                $department_id = $filter['department_id'];
                unset($filter['department_id']);
                $this->request->get(['filter' => json_encode($filter)]);
            }
            if ($department_id) {
                $admin_ids = $this->dadminModel->where('department_id', 'in', $department_id)->column('admin_id');
                $filter_w['id'] = ['in', $admin_ids];
            }


            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->where($filter_w)
                ->order($sort, $order)->fetchSql(false)
                ->count();
            $list = $this->model
                ->where($where)
                ->with(['dadmin.department'])
                ->with(['groups.getGroup'])
                ->where($filter_w)
                ->order($sort, $order)
                ->limit($offset, $limit)->fetchSql(false)
                ->select();


            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     * @return string|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        $groupdata = DepartmentModel::getGroupdata(
            $this->childrenGroupIds,
            $this->auth->isSuperAdmin() ? null : $this->auth->getGroups()
        );

        if ($this->request->isPost()) {

            $adminModel = new AdminModel();
            $departmentModel = new DepartmentModel();

            $params = $this->request->post("row/a");
            if ($params) {
                Db::startTrans();
                try {
                    $department_id = $this->request->post("department_id/a");

                    //获取部门信息
                    if (!$department_id) {
                        exception(__("部门不能为空"));
                    }
                    $d_list = $departmentModel->where('id', 'in', $department_id)->select();

                    if (!$d_list) {
                        exception(__("部门不能为空"));
                    }

                    if (!Validate::is($params['password'], '\S{6,16}')) {
                        exception(__("Please input correct password"));
                    }
                    $params['salt'] = Random::alnum();
                    $params['password'] = md5(md5($params['password']) . $params['salt']);
                    $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
                    $result = $adminModel->validate('Admin.add')->save($params);
                    if ($result === false) {
                        exception($adminModel->getError());
                    }

                    $admin_id = $adminModel->id;
                    $dadmin = array();
                    //添加部门信息
                    foreach ($d_list as $d_row) {
                        $dadmin[] = ['department_id' => $d_row->id, 'organise_id' => $d_row->organise_id ? $d_row->organise_id : $d_row->id, 'admin_id' => $admin_id];
                    }
                    $this->dadminModel->saveAll($dadmin);

                    $group = $this->request->post("group/a");
                    //过滤不允许的组别,避免越权
                    $group = array_intersect($this->childrenGroupIds, $group);

                    //添加权限默认第一个部门
                    if (!$group) {
                        exception(__('The parent group exceeds permission limit'));
                    }

                    $dataset = [];
                    foreach ($group as $value) {
                        $dataset[] = ['uid' => $admin_id, 'group_id' => $value];
                    }
                    model('AuthGroupAccess')->saveAll($dataset);


                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign('groupdata', $groupdata);
        return $this->view->fetch();
    }

    /**
     * 修改
     * @param null $ids
     * @return string|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit($ids = null)
    {
        $this->model = new AdminModel();
        $departmentModel = new DepartmentModel();
        $row = $this->model->get($ids);
        if ($this->request->isPost()) {
            //判断是不是超级管理员编辑
            if (!DepartmentAdminModel::isSuperAdmin($this->auth->id) && DepartmentAdminModel::isSuperAdmin($row->id)) {
                $this->error("您无权操作超级管理员");
            }

            $params = $this->request->post("row/a");

            if ($params) {
                Db::startTrans();
                try {
                    $department_id = $this->request->post("department_id/a");
                    //获取部门信息
                    if (!$department_id) {
                        exception(__("Department can't null"));
                    }
                    $d_list = $departmentModel->where('id', 'in', $department_id)->select();

                    if (!$d_list) {
                        exception(__("Department can't null"));
                    }

                    if ($params['password']) {
                        if (!Validate::is($params['password'], '\S{6,16}')) {
                            exception(__("Please input correct password"));
                        }
                        $params['salt'] = Random::alnum();
                        $params['password'] = md5(md5($params['password']) . $params['salt']);
                    } else {
                        unset($params['password'], $params['salt']);
                    }
                    //这里需要针对username和email做唯一验证
                    $adminValidate = \think\Loader::validate('Admin');
                    $adminValidate->rule([
                        'username' => 'require|regex:\w{3,12}|unique:admin,username,' . $row->id,
                        'email' => 'require|email|unique:admin,email,' . $row->id,
                        'password' => 'regex:\S{32}',
                    ]);
                    $result = $row->validate('Admin.edit')->save($params);
                    if ($result === false) {
                        exception($row->getError());
                    }
                    $exist_departmentids = $this->dadminModel->where('admin_id', $row->id)->column('department_id');
                    $dadmin = array();
                    $deleteids = array_diff($exist_departmentids, $department_id);
                    //添加部门信息
                    foreach ($d_list as $d_row) {
                        if (!in_array($d_row->id, $exist_departmentids)) {
                            $dadmin[] = ['department_id' => $d_row->id, 'organise_id' => $d_row->organise_id ? $d_row->organise_id : $d_row->id, 'admin_id' => $row->id];
                        }
                    }
                    if ($deleteids) {
                        $this->dadminModel->where('admin_id', $row->id)->where('department_id', 'in', $deleteids)->delete();
                    }
                    if (count($dadmin) > 0) {
                        $this->dadminModel->saveAll($dadmin);
                    }

                    // 先移除所有权限
                    model('AuthGroupAccess')->where('uid', $row->id)->delete();

                    $group = $this->request->post("group/a");
                    // 过滤不允许的组别,避免越权
                    $group = array_intersect($this->childrenGroupIds, $group);
                    if (!$group) {
                        exception(__('The parent group exceeds permission limit'));
                    }

                    $dataset = [];
                    foreach ($group as $value) {
                        $dataset[] = ['uid' => $row->id, 'group_id' => $value];
                    }
                    model('AuthGroupAccess')->saveAll($dataset);

                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $groupdata = DepartmentModel::getGroupdata(
            $this->childrenGroupIds,
            $this->auth->isSuperAdmin() ? null : $this->auth->getGroups()
        );
        $grouplist = $this->auth->getGroups($row['id']);
        $groupids = [];
        foreach ($grouplist as $k => $v) {
            $groupids[] = $v['id'];
        }
        $this->view->assign("groupids", $groupids);

        $this->view->assign('row', $row);
        $this->view->assign('department_ids', $this->dadminModel->getDepartmentIds($ids));
        $this->view->assign('groupdata', $groupdata);
        return $this->view->fetch();
    }


    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $this->childrenAdminIds = $this->auth->getChildrenAdminIds($this->auth->isSuperAdmin());
        $this->model = new AdminModel();

        $ids = $ids ? $ids : $this->request->post("ids");
        if ($ids) {
            $ids = array_intersect($this->childrenAdminIds, array_filter(explode(',', $ids)));
            // 避免越权删除管理员
            $childrenGroupIds = $this->childrenGroupIds;
            $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function ($query) use ($childrenGroupIds) {
                $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
            })->select();
            if ($adminList) {
                $deleteIds = [];
                foreach ($adminList as $k => $v) {
                    $deleteIds[] = $v->id;
                }
                $deleteIds = array_values(array_diff($deleteIds, [$this->auth->id]));
                if ($deleteIds) {
                    Db::startTrans();
                    try {
                        $this->model->destroy($deleteIds);
                        model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
                        //删除部门员工信息
                        $this->dadminModel->where('admin_id', 'in', $deleteIds)->delete();;

                        Db::commit();
                    } catch (\Exception $e) {
                        Db::rollback();
                        $this->error($e->getMessage());
                    }
                    $this->success();
                }
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('You have no permission'));
    }

    /**
     * 批量更新
     * @internal
     */
    public function multi($ids = "")
    {
        // 管理员禁止批量操作
        $this->error();
    }

    /**
     * 设置部门负责人
     */
    public function principal($ids = "")
    {

        $this->model = new AdminModel();
        $departmentModel = new DepartmentModel();
        $row = $this->model->get($ids);
        if ($this->request->isPost()) {

            //判断是不是超级管理员编辑
            if (!DepartmentAdminModel::isSuperAdmin($this->auth->id) && DepartmentAdminModel::isSuperAdmin($row->id)) {
                $this->error("您无权操作超级管理员");
            }

            $department_id = $this->request->post("department_id/a");
            if ($department_id && $department_id[0]) {
                Db::startTrans();
                try {
                    $d_list = $departmentModel->where('id', 'in', $department_id)->select();

                    if (!$d_list) {
                        exception(__("Department can't null"));
                    }
                    //先移除他所有负责的部门
                    $this->dadminModel->where('admin_id', $row->id)->update(['is_principal' => 0]);

                    //判断选择的部门是否存在，不存在就先把他加入
                    $p_departmentids = array_column($d_list, 'id');
                    $exist_departmentids = $this->dadminModel->where('admin_id', $row->id)->column('department_id');

                    $dadmin = array();
                    //添加部门信息
                    foreach ($d_list as $d_row) {
                        if (!in_array($d_row->id, $exist_departmentids)) {
                            $dadmin[] = ['department_id' => $d_row->id, 'organise_id' => $d_row->organise_id ? $d_row->organise_id : $d_row->id, 'admin_id' => $row->id];
                        }
                    }
                    if (count($dadmin) > 0) {
                        $this->dadminModel->saveAll($dadmin);
                    }
                    //更改为负责人
                    $this->dadminModel->where('admin_id', $row->id)->where('department_id', 'in', $p_departmentids)->update(['is_principal' => 1]);


                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }

            } else {
                //移除他所有负责的部门
                $this->dadminModel->where('admin_id', $row->id)->update(['is_principal' => 0]);
            }
            $this->success();
        }


        $this->view->assign('row', $row);
        $this->view->assign('department_ids', $this->dadminModel::getPrincipalIds($ids));
        return $this->view->fetch();
    }

    /**
     * 同步
     */
    public function synchronism()
    {
        $allRow    = 5000;
        $PageCount = 10;
        //初始化
        $currentPage = 1; //当前页为1
        $currentRow  = 0; //当前行为0
        while($currentPage <= $PageCount) {
            $insert = [];
            $groupinsert = [];
            //计算当前页待处理行数
            if($allRow - $currentRow >1000) {
                //剩余处理行数大于1000行
                $maxRow =  $currentRow + 1000;
            }
            else {
                $maxRow =  intval($allRow);
            }

            //获取数据
            $url = 'http://223.2.199.169:8888/getREST_TYJK_JZGXXV2_4d23e7f354c24244b245f90641a96bae';
            $header=['applyId:00000005036_1661920559819','secretKey:1237d02d-0496-40c0-97d1-40d4bbfb3968','Content-Type:application/json'];
            $params  =['YXZT'=>'有效'];
            $content =['format'=>'JSON','page'=>$currentPage,'pagesize'=>1000,'params'=>$params];
            Log::info("当前页：".$currentPage);
            $data = constant\Common::getSynchronism($url,$header,$content);

//            if(empty($data)) {
//                $file = '/uploads/20220917/3967a19b7009fd8a03fdd207704b6d1e.json';
//                $filePath = ROOT_PATH . DS . 'public' . DS . $file;
//                if (!is_file($filePath)) {
//                    $this->error(__('No results were found'));
//                }
//                $data = json_decode(file_get_contents($filePath),true);
//                return false;
//            }

            $row =[];
            $relationship = [];
            $group = [];
            if(isset($data['status']) && $data['status'] == 200 && isset($data['data']['Rows']) && !empty($data['data']['Rows'])) {
                //获取最新数量
                $allRow      = $data['total'];
                Log::info("总条数：".$allRow);
                $PageCount   = $allRow/1000+1;   //以1000条为一页处理数据，获取总页数
                foreach ($data['data']['Rows'] as $k => $v) {
                    $adminid = 0;
//                    if(isset($v['SFZJH']) && !empty($v['SFZJH'])) {
//                        $adminid = Db::name('admin')->where(['no_number' => $v['SFZJH']])->value('id');
//                    }
//                    elseif (isset($v['ZGH']) && !empty($v['ZGH'])) {
//                        $adminid = Db::name('admin')->where(['username' => $v['ZGH']])->value('id');
//                    }

                    if (isset($v['ZGH']) && !empty($v['ZGH'])) {
                        $adminid = Db::name('admin')->where(['username' => $v['ZGH']])->value('id');
                    }
					
					//增量更新

                    if($adminid <1) {
                        $row['major']               = $v['EJXKDM_TEXT'];     //二级学科（专业）
                        $row['email']               = $v['DZXX'];            //电子邮箱
                        $row['mobile']              = $v['SJH'];             //手机号
                        $row['salt']                = 'wsdrjN';
						$row['avatar']              = '/assets/img/avatar.png'; //设置新管理员默认头像。
                        //$row['password']            = substr($row['mobile'],-6); // 密码为手机号后6位
						$row['password']            = 'a13ca3d4b7bc8b9984d8a181cb630c9d'; // 固定密码
                        //$row['password']            = md5(md5($row['password']).$row['salt']);
                        $row['employee_number']     = $v['ZGH'];             //职工号
                        $row['nickname']            = $v['XM'];              //姓名
                        $row['no_number']           = $v['SFZJH'];           //身份证件号
                        $row['education_code']      = $v['ZGXLDM'];          //最高学历代码
                        $row['degree_code']         = $v['XWDM'];            //学位代码
                        $row['research_direction']  = $v['YJFX'];            //研究方向
                        $row['education']           = $v['ZGXLDM_TEXT'];     //最高学历
                        $row['degree']              = $v['XWDM_TEXT'];       //学位
                        $row['professional_ranks']  = $v['ZYJSZWDM_TEXT'];   //专业技术职务（职称）
                        $row['createtime']          = time();
                        $row['createtime']          = time();
                        $row['updatetime']          = time();
                        $row['status']              = 'normal';

                        //插入职工数据
                        $row['username']            = $v['ZGH'];             //用户名为工号
                        $adminid = Db::name('admin')->insertGetId($row);

                        //更新职工与部门关系
                        if(isset($v['SZDWDM']) && !empty($v['SZDWDM'])) {
                            $departmentid = Db::name('department')->where(['code' => $v['SZDWDM']])->value('id');
                            $relationship['department_id'] = $departmentid;
                            $relationship['organise_id']   = $departmentid;
                            $relationship['admin_id']      = $adminid;
                            $relationship['create_time']   = time();
                            $relationship['update_time']   = time();
                            $insert[] = $relationship;
                        }

                        //更新职工与角色组关系
                        $group['uid']        = $adminid;
                        $group['group_id']   = 8;
                        $groupinsert[] = $group;
                    }
                    else {
                        //Db::name('admin')->where('id', $adminid)->update($row);
                    }
                }
                Db::name("department_admin")->insertAll($insert);
                Db::name("auth_group_access")->insertAll($groupinsert);
            }
            elseif(isset($data['status']) && $data['status'] == 200) {
                if(isset($data['data']['Rows']) && !empty($data['data']['Rows'])) {
                    Log::info("有问题：".json_decode($data));
                }
                else {
                    Log::info("无数据,消息：".$data['msg']."错误码：".$data['status']);
                }
            }
            else {
                if(isset($data['msg']) && $data['status'] != 200) {
                    Log::info("错误信息：".$data['msg']."错误码：".$data['status']);
                }
                return false;
            }
            $currentPage++; //当前页导入完成，当前页+1
            $currentRow = $maxRow; //当前行等于当前最大处理行+1
        }
        return true;
    }
}
